﻿
using EnvCheck.Services;
using System;
using System.Windows;
using System.Windows.Controls;

using System.ComponentModel;
using EnvCheck.Models;


namespace EnvCheck.View.Export
{
    /// <summary>
    /// Interaction logic for CurrentEnv.xaml
    /// </summary>
    public partial class CurrentEnv : UserControl
    {
        private CurrentEnvViewModel viewModel = new();
        public CurrentEnv()
        {
            InitializeComponent();
            // DataContext = viewModel;
        }
        private void SaveVersion_Click(object sender, RoutedEventArgs e)
        {
            if (DataContext is not CurrentEnvViewModel vm || string.IsNullOrWhiteSpace(vm.NewVersionName))
            {
                MessageBox.Show("请输入有效的版本号！");
                return;
            }
            var existing = DatabaseService.IsVersionAlreadyExisting(vm.NewVersionName);
            if (existing)
            {
                var result = MessageBox.Show(
                    $"版本号 {vm.NewVersionName} 已存在，是否覆盖？",
                    "确认覆盖",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Warning
                );

                if (result == MessageBoxResult.No)
                {
                    // 用户选择不覆盖，直接返回
                    return;
                }
                DatabaseService.DeleteVersionAndRelatedApps(vm.NewVersionName);
            }


            try
            {
                var EnvToSave = vm.InstalledApps.ToList();
                DatabaseService.SaveAppReqs(vm.NewVersionName, EnvToSave);
                if (existing)
                {
                    GlobalViewModel.ModelByReqVersion.Remove(vm.NewVersionName);
                    GlobalViewModel.ModelByReqVersion.Add(vm.NewVersionName, []);
                    foreach (var item in EnvToSave) { 
                        AppReqSatisfactionStatus status=new AppReqSatisfactionStatus
                        {
                            Name = item.Name,
                            ExpectedVersion = item.ExpectedVersion,
                            InstalledVersion  = item.ExpectedVersion,
                            VersionPolicyDisplay = item.VersionPolicyDisplay
                        };
                        GlobalViewModel.ModelByReqVersion[vm.NewVersionName].Add(status.Name, status);
                    }
                    
                }
                MessageBox.Show("保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存失败：{ex.Message}");
                Serilog.Log.Error($"保存失败:{ex}\n{ex.StackTrace}");
            }
        }
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var tb = sender as TextBox;
            string currentText = tb?.Text;
            if((currentText??"").Trim().Length > 0)
            {
                viewModel.NewVersionName = currentText;
            }
        }
        private void ViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SomeProperty")
            {
                // 做一些事情，比如更新界面
            }
        }


    }
}

